import {
  ProForm,
  ProFormDependency,
  ProFormSelect,
  ProFormText,

} from '@ant-design/pro-components';
import { message } from 'antd';
import {useModel} from 'umi';
import {modifypersonal} from "@/services/ant-design-pro/api";
import {history} from "@@/core/history";



export default () => {

  const { initialState } = useModel('@@initialState');
  // @ts-ignore
  const { currentUser } = initialState;

  const handleSubmit = async (values: API.UserModifyParams) => {
    try {
      // 修改
      const success = await modifypersonal(values);
      if (success) {
        const defaultLoginSuccessMessage = '修改成功！';
        message.success(defaultLoginSuccessMessage);

        /** 此方法会跳转到 redirect 参数所在的位置 */
        if (!history) return;
        const {query} = history.location;
        history.push({
          pathname: '/welcome',
          query,
        });
        return;
      }
    } catch (error: any) {
      const defaultLoginFailureMessage = '修改失败，请重试！';
      message.error(defaultLoginFailureMessage);
    }
  };

  return (
    <ProForm
      onFinish={async (values) => {
        await handleSubmit(values as API.UserModifyParams);
        message.success('提交成功');
      }}
      initialValues={{
        gender: currentUser.gender,
      }}
    >

      <ProFormText
        width="md"
        name="username"
        label="用户名"
        tooltip="最长为 24 位"
        initialValue={currentUser.username} // 设置初始值
      />
      <ProFormText
        width="md"
        name="phone"
        label="手机号"
        tooltip="最长为 24 位"
        initialValue={currentUser.phone}
      />
      <ProFormText
        width="md"
        name="email"
        label="邮箱"
        tooltip="最长为 24 位"
        initialValue={currentUser.email}
      />
      <ProFormText
        width="md"
        name="durianCode"
        label="榴莲编号"
        readonly
        initialValue={currentUser.durianCode} // 设置初始值
      />

      <ProFormDependency name={['gender']}>
        {(gender) => (
          <ProFormSelect
            options={[
              {
                value: 1,
                label: '男',
              },
              {
                value: 2,
                label: '女',
              },
            ]}
            width="md"
            name="gender"
            label={`性别`}
            // @ts-ignore
            defaultValue={gender} // 根据当前用户的性别信息设置默认值
          />
        )}
      </ProFormDependency>
    </ProForm>
  );
};
